package com.coder520.sync_learning.demo;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

public class ShutDown {
	public static void main(String[] args) throws Exception {
		Runner oneRunner=new Runner();
		Thread countThread=new Thread(oneRunner,"CountThread");
		countThread.start();
//		睡眠1秒，main线程对countThread进行中断，使countThread能够感知中断而结束
		TimeUnit.SECONDS.sleep(10);
		countThread.interrupt();
		Runner runner=new Runner();
		countThread=new Thread(runner,"CountThread");
		
		countThread.start();
//		睡眠一秒，main线程对runner two 进行取消，使得countthread 能够感知on为false而结束
		TimeUnit.SECONDS.sleep(10);
		runner.cancel();
	}
	private static class Runner implements Runnable{
		private long i;
		private volatile boolean on=true;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			while (on&&!Thread.currentThread().isInterrupted()) {
				i++;
			}
			System.out.println("Count i="+i);
		}
		
		public void cancel() {
			// TODO Auto-generated method stub
			on=false;
		}
		
	}
}
